﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for clsJobsAppliedDAL
/// </summary>
public class clsJobsAppliedDAL
{
    InstantNaukriDataContextDataContext objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
    public clsJobsAppliedDAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public IN_TB_JOB_APPLIED GetJobsAppliedById(int Id)
    {
        try
        {
            IN_TB_JOB_APPLIED objIN_TB_JOB_APPLIEDList = new IN_TB_JOB_APPLIED();
            objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
            objIN_TB_JOB_APPLIEDList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                        where obj.ID == Id
                                        select obj).SingleOrDefault();
            return objIN_TB_JOB_APPLIEDList;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public List<IN_TB_JOB_APPLIED> GetJobsAppliedByUserId(int UserId)
    {
        try
        {
            List<IN_TB_JOB_APPLIED> objIN_TB_JOB_APPLIEDList = new List<IN_TB_JOB_APPLIED>();
            objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
            objIN_TB_JOB_APPLIEDList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                        where obj.USER_ID == UserId
                                        select obj).ToList();
            return objIN_TB_JOB_APPLIEDList;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public bool IsJobApplied(int UserId, int Job_Post_Id)
    {
        try
        {
            IN_TB_JOB_APPLIED objIN_TB_JOB_APPLIEDList = new IN_TB_JOB_APPLIED();
            objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
            objIN_TB_JOB_APPLIEDList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                        where obj.USER_ID == UserId && obj.JOB_ID == Job_Post_Id
                                        select obj).SingleOrDefault();
            if (objIN_TB_JOB_APPLIEDList != null)
                return true;
            else
                return false;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public IN_TB_JOB_APPLIED GetJobStatusByUser(int UserId, int Job_Post_Id)
    {
        try
        {
            IN_TB_JOB_APPLIED objIN_TB_JOB_APPLIEDList = new IN_TB_JOB_APPLIED();
            objInstantNaukriDataContextDataContext = new InstantNaukriDataContextDataContext();
            objIN_TB_JOB_APPLIEDList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                        where obj.USER_ID == UserId && obj.JOB_ID == Job_Post_Id
                                        select obj).SingleOrDefault();
            return objIN_TB_JOB_APPLIEDList;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public int SaveJobsApplied(IN_TB_JOB_APPLIED objIN_TB_JOB_APPLIEDOLD)
    {
        try
        {
            IN_TB_JOB_APPLIED objIN_TB_JOB_APPLIEDNEW = new IN_TB_JOB_APPLIED();
            objIN_TB_JOB_APPLIEDNEW = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                       where obj.ID == objIN_TB_JOB_APPLIEDOLD.ID
                                       select obj).SingleOrDefault();
            if (objIN_TB_JOB_APPLIEDNEW != null)
            {
                objIN_TB_JOB_APPLIEDNEW.JOB_ID = objIN_TB_JOB_APPLIEDOLD.JOB_ID;
                objIN_TB_JOB_APPLIEDNEW.STATUS = objIN_TB_JOB_APPLIEDOLD.STATUS;
                objIN_TB_JOB_APPLIEDNEW.USER_ID = objIN_TB_JOB_APPLIEDOLD.USER_ID;
                objIN_TB_JOB_APPLIEDNEW.MODIFIED_ON = objIN_TB_JOB_APPLIEDOLD.MODIFIED_ON;
                objIN_TB_JOB_APPLIEDNEW.EMPLOYER_COMMENT = objIN_TB_JOB_APPLIEDOLD.EMPLOYER_COMMENT;
                objIN_TB_JOB_APPLIEDNEW.APPLIED_ON = objIN_TB_JOB_APPLIEDOLD.APPLIED_ON;
                objInstantNaukriDataContextDataContext.SubmitChanges();
                return objIN_TB_JOB_APPLIEDNEW.ID;
            }
            else
            {
                objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs.InsertOnSubmit(objIN_TB_JOB_APPLIEDOLD);
                objInstantNaukriDataContextDataContext.SubmitChanges();
                return objIN_TB_JOB_APPLIEDOLD.ID;
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    public List<clsJobApplied> GetAppliedJobByEmployerId(int EmployerId, int UserId)
    {
        try
        {
            List<clsJobApplied> objclsJobAppliedList = new List<clsJobApplied>();
            objclsJobAppliedList = (from obj in objInstantNaukriDataContextDataContext.IN_TB_JOB_APPLIEDs
                                    join objJobPost in objInstantNaukriDataContextDataContext.IN_TB_JOB_POSTs on obj.JOB_ID equals objJobPost.ID
                                    join objEmployer in objInstantNaukriDataContextDataContext.IN_TB_EMPLOYER_MSTs on objJobPost.EMPLOYER_ID equals objEmployer.ID
                                    join objUser in objInstantNaukriDataContextDataContext.IN_TB_USERs on objEmployer.USER_ID equals objUser.ID
                                    join objResume in objInstantNaukriDataContextDataContext.IN_TB_RESUME_MSTs on obj.USER_ID equals objResume.USER_ID
                                    join objUserProfile in objInstantNaukriDataContextDataContext.IN_TB_PROFILEs on obj.USER_ID equals objUserProfile.USER_ID
                                    where objUser.ID == UserId
                                    select new clsJobApplied
                                    {
                                        CANDIDATE_NAME = objUserProfile.F_NAME + " " + objUserProfile.M_NAME + " " + objUserProfile.L_NAME,
                                        JOB_ID = (int)obj.JOB_ID,
                                        EMAIL = objUser.EMAIL,
                                        MOBILE = objUser.MOBILE,
                                        PHONE = objUserProfile.PHONE,
                                        EXPERIENCE = (int)objResume.TOTAL_EXP,
                                        JOB_TITLE = objJobPost.JOB_TITLE,
                                        JOB_DESCRIPTION = objJobPost.JOB_DESCRIPTION,
                                        JOB_LOCATION = (int)objJobPost.JOB_LOCATION,
                                        SKILLS_REQUIRED = objJobPost.SKILLS_REQUIRED,
                                        FUNCTIONAL_AREA = (int)objJobPost.FUNCTIONAL_AREA,
                                        JOB_POSTED_ON = (DateTime)objJobPost.CREATED_ON,
                                        COMMENT = obj.EMPLOYER_COMMENT,
                                        STATUS = (int)obj.STATUS,
                                        JOB_APPLIED_ID = (int)obj.ID
                                    }).ToList();
            return objclsJobAppliedList;
        }
        catch (Exception)
        {

            throw;
        }
    }

}